package com.gamatechno.solodestination;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.gamatechno.adapter.LazyAdapterTab;
import com.gamatechno.solodestination.model.HttpUtils;
import com.gamatechno.solodestination.model.Komentar;
import com.gamatechno.solodestination.model.Kuliner;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Tab extends ListActivity {
	public final static String KEY_TES = "tes";
	public final static String KEY_IMG = "img";
	public final static String KEY_NAMA = "nama";
	public final static String KEY_TGL = "tgl";
	public final static String KEY_TEXT = "text";
	public final static String KEY_RATING = "rating";
	LazyAdapterTab adapter;
	String ex="0";
	String id_detail="0";

	private class GetKomentar extends AsyncTask<Void, Void, ArrayList<Komentar>> {

		ProgressDialog dialog = new ProgressDialog(Tab.this);

		protected void onPreExecute() {
//			dialog.setMessage(" Loading... ");
//			dialog.show();
		}
		
		@Override
		protected ArrayList<Komentar> doInBackground(Void... params) {
			ArrayList<Komentar> lstkom = new ArrayList<Komentar>();

			String url = "http://sms.gamatechno.com/solo_destination/services/getKuliner.php?method=getcomment&kuliner_id="+id_detail+"&format=json";
			

			Log.i("url", url);
			try {
				HttpUtils u = new HttpUtils();
				String a = u.getContents(url);
				Log.i("content", a);
				JSONObject obj = new JSONObject(a);
				Log.i("JSONObject content length", obj.length() + "");
				String jo = obj.get("result") + "";
				Log.i("isi result", jo + "");
				JSONArray ka = new JSONArray(jo);
				Log.i("result length", ka.length() + "");

				for (int i = 0; i < ka.length(); i++) {
					JSONObject ja = ka.getJSONObject(i);
					Komentar kom = new Komentar();
					kom.setUser_id("user_id");
					kom.setName("name");
					kom.setKuliner_id("kuliner_id");
					kom.setRating("rating");
					kom.setUser_comment("user_comment");
					kom.setComment("comment");
					
					lstkom.add(kom);

				}

			} catch (Exception e) {
				// e.printStackTrace();
				Log.i("i", e.toString());
			}

			return lstkom;

		}

		@Override
		protected void onPostExecute(ArrayList<Komentar> result) {
			dialog.dismiss();
			setKomentar(result);
			// setSuhu();
			// super.onPostExecute(result);
		}
	}
	
	public void setKomentar(ArrayList<Komentar> result)
	{
		adapter = new LazyAdapterTab(this, result);
		getListView().setAdapter(adapter);
		setListViewHeightBasedOnChildren(getListView());
		
	}
	
	@Override
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
		  return;
		}
		// get data via the key
		ex = extras.getString("i");
		id_detail = extras.getString("id_detail");
		if (ex != null) {
		  Log.i("i",ex);
		  Log.i("id_detail",id_detail);
		} 
		
		
		setContentView(R.layout.activity_tab);
		
		new GetKomentar().execute();
		
//		ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
////		for (int i = 0; i < 12; i++) {
////
////			HashMap<String, String> map = new HashMap<String, String>();
////			map.put(KEY_TES, "tes");
////			data.add(map);
////
////		}
//		if(ex.equals("1"))
//		{
//			HashMap<String, String> map = new HashMap<String, String>();
//			map.put(KEY_TES, "tes");
//			map.put(KEY_IMG, String.valueOf(R.drawable.hafiz));
//			map.put(KEY_NAMA, "Hafiz Ridha");
//			map.put(KEY_TGL, "Sunday, 23 Maret 2014");
//			map.put(KEY_TEXT, "Baru pertama. Enak rasanya");
//			map.put(KEY_RATING, "3.5");
//			data.add(map);
//			
//			HashMap<String, String> map2 = new HashMap<String, String>();
//			map2.put(KEY_TES, "tes");
//			map2.put(KEY_IMG, String.valueOf(R.drawable.rezha));
//			map2.put(KEY_NAMA, "Hinova Rezha");
//			map2.put(KEY_TGL, "Driday, 26 Maret 2014");
//			map2.put(KEY_TEXT, "Mak nyuss");
//			map2.put(KEY_RATING, "4.3");
//			data.add(map2);
//			
//			HashMap<String, String> map3 = new HashMap<String, String>();
//			map3.put(KEY_TES, "tes");
//			map3.put(KEY_IMG, String.valueOf(R.drawable.iwak));
//			map3.put(KEY_NAMA, "Rakhmad Ikhsanudin");
//			map3.put(KEY_TGL, "Saturday, 28 Maret 2014");
//			map3.put(KEY_TEXT, "Enak Mantab Jos");
//			map3.put(KEY_RATING, "5");
//			data.add(map3);
//		}else if(ex.equals("2"))
//		{
//			HashMap<String, String> map = new HashMap<String, String>();
//			map.put(KEY_TES, "tes");
//			map.put(KEY_IMG, String.valueOf(R.drawable.akunku_icon));
//			map.put(KEY_NAMA, "Davi");
//			map.put(KEY_TGL, "Sunday, 23 Maret 2014");
//			map.put(KEY_TEXT, "Enak tapi biasa");
//			map.put(KEY_RATING, "2");
//			data.add(map);
//			
//			HashMap<String, String> map2 = new HashMap<String, String>();
//			map2.put(KEY_TES, "tes");
//			map2.put(KEY_IMG, String.valueOf(R.drawable.akunku_icon));
//			map2.put(KEY_NAMA, "Husk");
//			map2.put(KEY_TGL, "Driday, 26 Maret 2014");
//			map2.put(KEY_TEXT, "Kalo lapar apa aja enak");
//			map2.put(KEY_RATING, "2");
//			data.add(map2);
//			
//			HashMap<String, String> map3 = new HashMap<String, String>();
//			map3.put(KEY_TES, "tes");
//			map3.put(KEY_IMG, String.valueOf(R.drawable.akunku_icon));
//			map3.put(KEY_NAMA, "Ryl");
//			map3.put(KEY_TGL, "Saturday, 28 Maret 2014");
//			map3.put(KEY_TEXT, "Enak sih enak. Tambah enak kalo gratis");
//			map3.put(KEY_RATING, "3");
//			data.add(map3);
//			
//			
//		}
		
		
	}
	
	public static void setListViewHeightBasedOnChildren(ListView listView) {
	    ListAdapter listAdapter = listView.getAdapter();
	    if (listAdapter == null)
	        return;

	    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
	    int totalHeight = 0;
	    View view = null;
	    for (int i = 0; i < listAdapter.getCount(); i++) {
	        view = listAdapter.getView(i, view, listView);
	        if (i == 0)
	            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

	        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
	        totalHeight += view.getMeasuredHeight();
	    }
	    ViewGroup.LayoutParams params = listView.getLayoutParams();
	    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
	    listView.setLayoutParams(params);
	    listView.requestLayout();
	    listView.setScrollContainer(false);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tab, menu);
		return true;
	}

}
